/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.hq.mes.bussiness.inrecord.service.impl;

import com.hq.mes.bussiness.inrecord.domain.Inrecord;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.hq.mes.bussiness.inrecord.repository.InrecordRepository;
import com.hq.mes.bussiness.inrecord.service.InrecordService;
import com.hq.mes.bussiness.inrecord.service.dto.InrecordDto;
import com.hq.mes.bussiness.inrecord.service.dto.InrecordQueryCriteria;
import com.hq.mes.bussiness.inrecord.service.mapstruct.InrecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wsq
* @date 2021-12-24
**/
@Service
@RequiredArgsConstructor
public class InrecordServiceImpl implements InrecordService {

    private final InrecordRepository inrecordRepository;
    private final InrecordMapper inrecordMapper;

    @Override
    public Map<String,Object> queryAll(InrecordQueryCriteria criteria, Pageable pageable){
        Page<Inrecord> page = inrecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(inrecordMapper::toDto));
    }

    @Override
    public List<InrecordDto> queryAll(InrecordQueryCriteria criteria){
        return inrecordMapper.toDto(inrecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public InrecordDto findById(Long id) {
        Inrecord inrecord = inrecordRepository.findById(id).orElseGet(Inrecord::new);
        ValidationUtil.isNull(inrecord.getId(),"Inrecord","id",id);
        return inrecordMapper.toDto(inrecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InrecordDto create(InrecordDto resources) {
        if(inrecordRepository.findByCode(resources.getCode()) != null){
            throw new EntityExistException(Inrecord.class,"code",resources.getCode());
        }
        return inrecordMapper.toDto(inrecordRepository.save( inrecordMapper.toEntity(resources)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InrecordDto resources) {
        Inrecord inrecord = inrecordRepository.findById(resources.getId()).orElseGet(Inrecord::new);
        ValidationUtil.isNull( inrecord.getId(),"Inrecord","id",resources.getId());
        Inrecord inrecord1 = null;
        inrecord1 = inrecordRepository.findByCode(resources.getCode());
        if(inrecord1 != null && !inrecord1.getId().equals(inrecord.getId())){
            throw new EntityExistException(Inrecord.class,"code",resources.getCode());
        }
        inrecord.copy( inrecordMapper.toEntity(resources));
        inrecordRepository.save(inrecord);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            inrecordRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<InrecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InrecordDto inrecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("编码", inrecord.getCode());
            map.put("名称", inrecord.getName());
            map.put("备注", inrecord.getRemark());
            map.put("创建人", inrecord.getCreateBy());
            map.put("创建时间", inrecord.getCreateTime());
            map.put("最后修改人", inrecord.getUpdateBy());
            map.put("最后修改时间", inrecord.getUpdateTime());
            map.put("是否已删除", inrecord.getIsDel());
            map.put(" version",  inrecord.getVersion());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}